package org.kaede.alimama;

import android.app.AlertDialog;
import android.app.Service;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.IBinder;
import android.view.WindowManager;
import android.widget.Toast;

import org.kaede.alimama.client.NettyClient;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import io.netty.channel.Channel;
import io.netty.util.internal.StringUtil;

public class AlimamaService extends Service {
    NettyClient nettyClient;
    Channel channel;
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    public AlimamaService() {
    }

    @Override
    public void onCreate() {
        nettyClient = new NettyClient(this);
        final ClipboardManager cb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        cb.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {

            @Override
            public void onPrimaryClipChanged() {
                //Toast.makeText(AlimamaService.this, getUrl(cb.getText().toString()), Toast.LENGTH_LONG).show();
                String content = cb.getText().toString();
                if (!StringUtil.isNullOrEmpty(content)) {
                    if (content.startsWith("淘宝商品")) {
                        final String url = getUrl(content);
                        if (url != null) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(AlimamaService.this).setTitle("提示")//设置对话框标题
                                    .setMessage("是否跳转到推广页面")//设置显示的内容
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加确定按钮

                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                                            executorService.submit(new Runnable() {
                                                @Override
                                                public void run() {
//                                                    if (channel == null)
//                                                        channel = nettyClient.connect("yehan.vicp.net", 4001);
//                                                    if (!channel.isActive())
//                                                        channel = nettyClient.connect("yehan.vicp.net", 4001);
                                                    channel = nettyClient.connect("yehan.vicp.net", 4001);
                                                    try {
                                                        channel.writeAndFlush(url).sync();
                                                    } catch (InterruptedException e) {
                                                        e.printStackTrace();
                                                    }
                                                }
                                            });
                                        }

                                    }).setNegativeButton("取消", null);
                            AlertDialog ad = builder.create();
                            ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                            ad.setCanceledOnTouchOutside(false);                                   //点击外面区域不会让dialog消失
                            ad.show();
                    }
                }
            }
        }
    });
        super.onCreate();
    }

    private String getUrl(String text)
    {
        int index = text.indexOf("http://");
        int i = index + 7;
        for (;i<text.length();i++)
            if(text.charAt(i) == ' ' || text.charAt(i) == '(')
                break;
        if(i > index+7 && i < text.length())
            return text.substring(index,i).trim();
        else
            return null;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
